public class test4 {
//力扣   34.在排序数组中查找元素的第一个位置和最后一个位置
    public int[] searchRange(int[] nums, int target) {
        int[] ret = new int[2];
        ret[0] = -1;
        ret[1] = -1;
        if(nums.length==0){
            return ret;
        }
        //寻找左端点
        int left = 0,right = nums.length-1;
        int mid = 0;
        while(left<right){
            mid = left + (right - left)/2;
            if(nums[mid]<target){
                left = mid + 1;
            }else{
                right = mid;
            }
        }
        //判断
        if(nums[left]!=target){
            return ret;
        }else{
            ret[0] = right;
        }
        //寻找右端点
        left = 0;right = nums.length-1;
        mid = 0;
        while(left<right){
            mid = left + (right - left+1)/2;
            if(nums[mid]<=target){
                left = mid;
            }else{
                right = mid - 1;
            }
        }
        ret[1] = right;
        return ret;
    }
}
